package com.sitech.ibnms.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.sitech.BaseConst;
import com.sitech.context.SpringContext;
import com.sitech.ibnms.util.AppException;
import com.sitech.ibnms.util.ErrorCodeConstants;
import com.sitech.portal.system.dao.ISysUserRoleDao;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;
import com.sitech.portal.system.domain.TblSYS_ROLE;
import com.sitech.webapp.action.BaseAction;


/**
 * 
* <p>Title: BaseIbnmsAction.java</p>
* <p>Description:***Action都要继承的类</p>
* <p>Copyright: Copyright (c) SITECH 2011</p>
* @author LJ
* @date Sep 30, 2011 11:14:55 AM
* @version V6.0
 */
public class BaseIbnmsAction extends BaseAction implements ErrorCodeConstants {

	protected static final long serialVersionUID = 1L;
	
	protected Map application;
    protected Map parameters;
    protected Map cookiesMap;
    protected String message;
    
    protected String activeID;
    public static final String AJAX_RETURN_TYPE = "ajax";
    public static final String FAILURE = "failure";
	protected Log log = LogFactory.getLog(getClass());

	/**
	 * 
	 * 
	 * @param ex
	 */
	protected void handleException(AppException ex) throws AppException{		  
			String message = "";			
			log.error("error", ex);
			ex.setMessage(message);		//待扩展	
			throw ex;		 
	}

	//得到当前用户
	public TblSYS_LOGINMSG  getCurrentUser()
	{
		
		return (TblSYS_LOGINMSG)ServletActionContext.getRequest().getSession().getAttribute(BaseConst.SESSION_USER_KEY);
	}
	//得到当前用户Session
	public HttpSession  getCurrentUserSession()
	{
		
		return (HttpSession)ServletActionContext.getRequest().getSession();
	}
	//得到用户关联的角色ID列表
	public List<String>  getCurrentUserRoleIdList() throws Exception
	{
		List<String> roleIdList=null;
		
		List<TblSYS_ROLE>  roleList=(List<TblSYS_ROLE>)ServletActionContext.getRequest().getSession().getAttribute(BaseConst.SESSION_USER_ROL_KEY);
		if(roleList==null || roleList.size()==0){
			ISysUserRoleDao sysUserRoleDao = (ISysUserRoleDao) SpringContext.getBean("sysUserRoleDao");
			roleList=(List<TblSYS_ROLE>)sysUserRoleDao.findRoleListByUserId(this.getCurrentUser().getUSER_ID());
			ServletActionContext.getRequest().getSession().setAttribute(BaseConst.SESSION_USER_ROL_KEY, roleList);
		}
		
		if(roleList!=null && roleList.size()>0)
		{
			roleIdList=new ArrayList();
			for(TblSYS_ROLE role:roleList)
			{
				roleIdList.add(role.getROLE_ID());
			}
		}
		return roleIdList;
	}

	public Map getApplication() {
		return application;
	}

	public void setApplication(Map application) {
		this.application = application;
	}

	public Map getParameters() {
		return parameters;
	}

	public void setParameters(Map parameters) {
		this.parameters = parameters;
	}

	public Map getCookiesMap() {
		return cookiesMap;
	}

	public void setCookiesMap(Map cookiesMap) {
		this.cookiesMap = cookiesMap;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
